Data Acquisition¶

Import Libraries, Function and Files¶

In [1]:
import io
import os
import pywt
import glob
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.signal as sig
from scipy import signal
from pywt import wavedec
from scipy.io import loadmat
from scipy.stats import entropy
from scipy.signal import filtfilt
from scipy.signal import butter, lfilter, welch
from sklearn import datasets, linear_model, metrics
In [2]:
# Mengakses seluruh csv file pada drive
raw_data = pd.read_csv('data sama.csv', header = None)

# Membuat dataframe
df_raw = pd.DataFrame(raw_data)
df_raw
C:\Users\CLARA\AppData\Local\Temp\ipykernel_2796\2629672973.py:2: DtypeWarning: Columns (46,48,139,146,148,154,155,165,178,185,205,207,216,219,221,226,227,228,245,257,275,278,279) have mixed types. Specify dtype option on import or set low_memory=False.
  raw_data = pd.read_csv('data sama.csv', header = None)
Out[2]:
0 1 2 3 4 5 6 7 8 9 ... 270 271 272 273 274 275 276 277 278 279
0 4498.974121 3972.820313 4404.615234 4195.384277 4249.230469 4412.820313 4278.974121 4551.794922 3853.333252 5752.820313 ... 4254.871582 4443.076660 4194.358887 4396.922852 3394.871826 3908.205078.1 4274.358887 4098.974121 4179.487305.1 4047.179443.2
1 4501.538574 3974.871582 4408.205078 4194.871582 4250.256348 4412.307617 4257.948730 4545.127930 3838.461426 5740.000000 ... 4253.846191 4445.127930 4190.256348 4389.230469 3385.128174 3902.05127 4269.230469 4089.230713 4175.897461 4003.5896
2 4502.051270 3983.589600 4409.230469 4195.897461 4247.692383 4415.384277 4253.333008 4533.846191 3834.871582 5707.179199 ... 4254.358887 4446.153809 4195.897461 4387.692383 3394.871826 3907.692139 4272.307617 4076.923096 4177.436035 3921.53833
3 4504.102539 3992.307617 4410.769043 4201.025391 4248.717773 4417.948730 4271.794922 4521.025391 3837.948730 5673.846191 ... 4256.922852 4445.127930 4194.871582 4392.307617 3416.923096 3918.974365 4284.102539 4082.051270 4188.205078 3922.05127
4 4504.102539 3984.102539 4412.307617 4200.000000 4248.205078 4418.974121 4276.922852 4506.153809 3834.871582 5654.358887 ... 4252.820313 4444.102539 4189.230469 4387.692383 3410.256348 3911.794678 4280.512695 4090.769043 4189.743652 3975.897217
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
7163 4546.153809 3981.538330 4456.410156 4245.641113 4283.076660 4489.743652 4332.307617 4650.256348 3811.281982 4320.512695 ... 4240.000000 4443.589844 4259.487305 4534.871582 3713.845947 4277.436035 4536.410156 3342.563965 4132.820313 643.589722
7164 4546.153809 3981.538330 4456.410156 4245.641113 4283.076660 4489.743652 4332.307617 4650.256348 3811.281982 4320.512695 ... 4240.000000 4443.589844 4259.487305 4534.871582 3713.845947 4277.436035 4536.410156 3342.563965 4132.820313 643.589722
7165 4546.153809 3981.538330 4456.410156 4245.641113 4283.076660 4489.743652 4332.307617 4650.256348 3811.281982 4320.512695 ... 4240.000000 4443.589844 4259.487305 4534.871582 3713.845947 4277.436035 4536.410156 3342.563965 4132.820313 643.589722
7166 4546.153809 3981.538330 4456.410156 4245.641113 4283.076660 4489.743652 4332.307617 4650.256348 3811.281982 4320.512695 ... 4240.000000 4443.589844 4259.487305 4534.871582 3713.845947 4277.436035 4536.410156 3342.563965 4132.820313 643.589722
7167 4546.153809 3981.538330 4456.410156 4245.641113 4283.076660 4489.743652 4332.307617 4650.256348 3811.281982 4320.512695 ... 4240.000000 4443.589844 4259.487305 4534.871582 3713.845947 4277.436035 4536.410156 3342.563965 4132.820313 643.589722

7168 rows × 280 columns

Cropping Data¶

In [3]:
# Set the sampling frequency in Hz
fs = 128

# Define the time ranges to crop
start_time = 20
end_time = 30
start_time1 = 40
end_time1 = 50

# Convert the time ranges to index ranges
start_idx = int(start_time * fs)
end_idx = int(end_time * fs)
start_idx1 = int(start_time1 * fs)
end_idx1 = int(end_time1 * fs)

# Crop the data to the first time range
hold_hand = df_raw.loc[start_idx:end_idx]

# Crop the data to the second time range
open_hand = df_raw.loc[start_idx1:end_idx1]

hold_hand
hold_hand.to_csv('Hold Hand Raw Data.csv')
In [4]:
open_hand
open_hand.to_csv('Open Hand Raw Data.csv')
In [5]:
hold_hand
Out[5]:
0 1 2 3 4 5 6 7 8 9 ... 270 271 272 273 274 275 276 277 278 279
2560 4517.435547 3978.974365 4173.846191 4195.897461 4254.871582 4436.410156 4157.948730 4554.871582 3529.230713 4273.333008 ... 4307.692383 4489.230469 4227.692383 4258.974121 3180.000000 3660.512695 4055.384521 3695.897461 3986.666504 1776.410156
2561 4512.307617 3971.794678 4163.076660 4198.974121 4251.794922 4436.922852 4145.127930 4542.563965 3521.538330 4263.589844 ... 4300.512695 4488.205078 4212.820313 4248.717773 3148.717773 3626.153809 4033.845947 3772.820313 3997.94873 2337.435791
2562 4512.307617 3976.410156 4157.948730 4198.974121 4251.794922 4437.435547 4153.333008 4540.512695 3512.307617 4258.461426 ... 4301.025391 4489.230469 4220.512695 4253.846191 3134.871826 3607.179443 4024.102539 3846.666504 4027.692139 2803.076904
2563 4515.384277 3984.615234 4161.025391 4195.897461 4251.281738 4436.922852 4167.692383 4550.256348 3519.487061 4268.205078 ... 4307.179199 4490.769043 4228.717773 4253.846191 3127.692139 3595.897461 4023.076904 3920.512695 4054.358887 3228.717773
2564 4514.871582 3968.205078 4161.025391 4194.871582 4248.717773 4438.974121 4150.769043 4546.153809 3526.666504 4268.717773 ... 4308.205078 4490.769043 4228.717773 4243.589844 3113.333252 3581.53833 4014.871582 3946.666504 4055.384521 3388.205078
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
3836 4506.666504 4009.230713 4415.384277 4208.717773 4264.615234 4317.948730 4260.512695 4583.076660 3811.281982 4314.871582 ... 4204.102539 4438.974121 4243.589844 4425.127930 3594.871826 4117.436035 4540.000000 4018.974365 4366.666504 3556.410156
3837 4505.641113 4009.743408 4413.333008 4206.666504 4262.563965 4311.794922 4258.461426 4584.102539 3812.307617 4313.846191 ... 4203.589844 4438.461426 4236.410156 4432.820313 3592.307617 4117.436035 4540.000000 4028.205078 4365.12793 3607.692139
3838 4506.153809 4008.717773 4410.769043 4201.025391 4261.538574 4310.256348 4262.563965 4589.230469 3818.461426 4315.384277 ... 4201.538574 4435.897461 4234.871582 4441.025391 3591.281982 4113.333008 4537.435547 4025.128174 4363.07666 3626.666504
3839 4505.641113 4007.692139 4409.743652 4202.051270 4262.563965 4311.794922 4266.666504 4584.102539 3815.897461 4305.641113 ... 4203.589844 4434.871582 4235.384277 4429.743652 3584.615234 4108.717773 4535.897461 4024.102539 4364.615234 3621.53833
3840 4505.641113 4010.769043 4412.307617 4207.692383 4264.102539 4313.846191 4280.512695 4586.666504 3813.333252 4310.256348 ... 4203.589844 4436.922852 4237.948730 4422.051270 3581.538330 4106.153809 4531.794922 4027.179443 4365.641113 3621.025635

1281 rows × 280 columns

In [6]:
open_hand
Out[6]:
0 1 2 3 4 5 6 7 8 9 ... 270 271 272 273 274 275 276 277 278 279
5120 4528.205078 3990.256348 4276.410156 4200.512695 4262.051270 4445.127930 4126.153809 4460.000000 3488.205078 4230.256348 ... 4265.127930 4457.435547 4211.281738 4398.461426 3554.358887 4067.692139 4460.000000 3993.845947 4296.410156 3170.769043
5121 4525.127930 3990.769043 4277.948730 4200.000000 4262.051270 4445.127930 4138.974121 4466.153809 3488.717773 4236.410156 ... 4261.538574 4453.333008 4208.717773 4407.692383 3564.102539 4083.076904 4473.333008 3976.923096 4294.358887 3046.666504
5122 4524.102539 3990.769043 4277.436035 4202.051270 4262.051270 4444.102539 4144.615234 4463.589844 3488.717773 4231.281738 ... 4261.025391 4453.333008 4205.127930 4410.256348 3549.230713 4080.512695 4468.717773 3956.923096 4286.153809 2944.615234
5123 4522.051270 3988.717773 4271.794922 4198.974121 4261.025391 4443.076660 4119.487305 4452.820313 3490.769043 4215.384277 ... 4265.127930 4459.487305 4214.358887 4413.846191 3548.205078 4084.615234 4471.281738 3967.179443 4294.358887 2969.743408
5124 4525.127930 3990.769043 4272.307617 4198.974121 4260.512695 4444.615234 4105.641113 4450.769043 3492.820313 4216.410156 ... 4265.127930 4462.051270 4224.615234 4415.897461 3551.794678 4083.076904 4471.794922 3976.923096 4294.871582 3023.5896
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
6396 4502.563965 3972.307617 4327.692383 4216.922852 4278.461426 4475.897461 4286.666504 4619.487305 3809.743408 4366.153809 ... 4244.615234 4438.461426 4262.051270 4478.461426 3696.923096 4230.769043 4601.025391 3945.640869 4408.717773 2916.923096
6397 4506.153809 3977.948730 4332.820313 4213.333008 4276.922852 4472.820313 4288.717773 4617.435547 3813.333252 4360.000000 ... 4241.538574 4435.384277 4259.487305 4494.358887 3688.717773 4228.205078 4598.974121 3954.871582 4404.615234 3003.5896
6398 4503.589844 3977.948730 4329.743652 4209.743652 4271.281738 4469.743652 4281.025391 4615.384277 3810.256348 4356.922852 ... 4245.641113 4437.948730 4256.922852 4492.307617 3682.563965 4217.94873 4596.922852 3983.076904 4411.281738 3177.94873
6399 4504.102539 3975.897217 4328.205078 4211.281738 4271.281738 4473.846191 4285.127930 4615.384277 3810.256348 4355.384277 ... 4244.615234 4437.948730 4257.436035 4473.846191 3680.512695 4207.179199 4590.256348 4003.589600 4417.94873 3286.666504
6400 4505.127930 3969.230713 4327.692383 4211.794922 4269.230469 4470.256348 4284.615234 4605.641113 3814.358887 4341.538574 ... 4240.512695 4434.358887 4255.384277 4468.205078 3675.384521 4200.0 4582.051270 4004.102539 4412.307617 3303.076904

1281 rows × 280 columns

Set Channel F3¶

In [7]:
hold_f3 = pd.read_csv('Data Hold Hand F3.csv', header = None)
hold_f3
Out[7]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 4173.846191 4272.820313 4504.615234 4340.000000 4192.307617 4264.615234 4381.538574 4302.563965 4319.487305 4405.641113 4426.666504 4333.333008 4361.538574 4104.102539 4333.333008 4200.512695 4401.025391 4250.769043 4374.871582 4326.666504
1 4163.076660 4274.871582 4498.461426 4344.102539 4192.820313 4264.102539 4376.410156 4302.051270 4318.974121 4404.615234 4430.769043 4336.922852 4357.948730 4112.820313 4330.769043 4196.922852 4413.846191 4258.974121 4366.666504 4326.153809
2 4157.948730 4284.102539 4496.410156 4342.051270 4193.333008 4268.205078 4375.897461 4302.051270 4314.358887 4405.641113 4438.974121 4329.743652 4356.922852 4112.307617 4327.692383 4193.333008 4416.922852 4260.000000 4363.076660 4326.153809
3 4161.025391 4292.307617 4494.358887 4340.512695 4188.205078 4272.307617 4378.461426 4303.589844 4321.025391 4402.051270 4437.948730 4322.051270 4361.538574 4105.641113 4308.205078 4196.922852 4410.256348 4250.769043 4365.127930 4331.794922
4 4161.025391 4295.384277 4483.589844 4342.563965 4189.230469 4271.794922 4380.000000 4301.538574 4328.717773 4404.615234 4438.461426 4326.153809 4363.589844 4108.717773 4300.000000 4201.538574 4408.205078 4248.717773 4362.563965 4333.333008
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1276 4415.384277 4380.512695 4365.641113 4420.000000 4110.769043 4342.051270 4396.922852 4402.563965 4326.666504 4306.153809 4322.563965 4454.358887 4241.025391 4188.717773 4288.205078 4373.333008 4318.974121 4340.000000 4181.025391 4167.692383
1277 4413.333008 4378.974121 4369.743652 4425.127930 4107.179199 4346.153809 4393.333008 4404.615234 4334.871582 4298.974121 4327.692383 4459.487305 4237.436035 4144.102539 4284.615234 4371.794922 4315.384277 4346.153809 4187.179199 4176.922852
1278 4410.769043 4374.871582 4362.563965 4430.256348 4117.436035 4350.769043 4391.281738 4400.000000 4336.922852 4265.641113 4330.769043 4447.179199 4241.025391 4118.461426 4283.589844 4371.281738 4322.563965 4351.794922 4191.794922 4175.897461
1279 4409.743652 4388.717773 4361.025391 4434.871582 4104.102539 4349.230469 4388.717773 4398.974121 4332.820313 4231.281738 4318.974121 4429.743652 4249.743652 4121.538574 4288.205078 4371.794922 4324.102539 4357.948730 4193.333008 4178.974121
1280 4412.307617 4392.820313 4361.538574 4432.307617 4084.102539 4351.281738 4386.666504 4404.102539 4334.871582 4230.769043 4302.051270 4427.179199 4250.256348 4129.230469 4289.230469 4370.256348 4317.436035 4362.051270 4193.846191 4188.717773

1281 rows × 20 columns

In [8]:
# Original Hold Hand F3
for column in hold_f3.columns:  # Loop melalui kolom-kolom DataFrame
  plt.figure()
  plt.title(f"Column-{column}")  # Display the custom title
  plt.plot(hold_f3[column])
  plt.show()
In [9]:
open_f3 = pd.read_csv('Data Open Hand F3.csv', header = None)
open_f3
Out[9]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 4276.410156 4288.205078 4447.179199 4604.615234 4016.410156 4143.076660 4317.948730 4304.615234 4308.717773 4241.538574 4328.717773 4373.846191 4174.871582 3983.076904 4224.102539 4227.692383 4334.871582 4289.743652 4283.076660 4397.948730
1 4277.948730 4290.256348 4450.256348 4598.974121 4006.666504 4146.153809 4317.436035 4329.230469 4303.589844 4240.512695 4333.846191 4380.000000 4183.589844 3988.205078 4229.230469 4216.922852 4331.281738 4302.051270 4287.179199 4404.615234
2 4277.436035 4293.333008 4447.692383 4602.051270 3978.461426 4148.205078 4324.102539 4348.717773 4305.127930 4247.179199 4331.794922 4375.384277 4194.871582 3990.256348 4235.384277 4212.307617 4331.794922 4303.076660 4293.333008 4402.563965
3 4271.794922 4277.948730 4450.256348 4605.127930 3980.000000 4148.205078 4328.205078 4343.076660 4309.743652 4251.794922 4334.871582 4364.102539 4201.025391 3960.512695 4235.384277 4204.102539 4335.897461 4297.948730 4291.281738 4406.153809
4 4272.307617 4253.846191 4455.384277 4606.666504 4002.563965 4150.256348 4326.666504 4335.897461 4310.256348 4257.436035 4340.512695 4360.000000 4207.179199 3875.384521 4234.871582 4182.563965 4337.948730 4295.897461 4285.127930 4407.692383
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1276 4327.692383 4288.205078 4378.974121 4306.666504 4198.974121 4513.333008 4376.410156 4350.256348 4421.538574 4371.281738 4405.641113 3423.589600 4358.461426 4174.871582 4325.127930 4155.384277 4331.794922 4380.000000 4346.666504 4256.922852
1277 4332.820313 4285.641113 4376.922852 4276.922852 4201.025391 4512.820313 4381.025391 4347.692383 4431.281738 4371.794922 4407.179199 3432.307617 4359.487305 4180.000000 4320.000000 4158.974121 4324.615234 4381.538574 4344.615234 4254.871582
1278 4329.743652 4292.820313 4378.461426 4262.051270 4204.615234 4517.948730 4381.538574 4347.692383 4431.794922 4371.281738 4407.179199 3447.179443 4357.948730 4184.615234 4316.922852 4160.512695 4316.410156 4383.589844 4346.666504 4252.307617
1279 4328.205078 4290.769043 4380.000000 4270.256348 4204.615234 4517.948730 4373.846191 4352.820313 4424.615234 4376.922852 4408.205078 3454.358887 4356.922852 4185.641113 4316.922852 4161.538574 4309.743652 4390.256348 4344.615234 4253.333008
1280 4327.692383 4285.127930 4381.538574 4276.922852 4201.025391 4515.897461 4370.769043 4353.846191 4422.563965 4373.846191 4409.230469 3458.461426 4357.948730 4185.641113 4321.025391 4169.230469 4295.897461 4389.230469 4337.948730 4253.846191

1281 rows × 20 columns

In [10]:
# Original Open F3
for column in open_f3.columns:  # Loop melalui kolom-kolom DataFrame
  plt.figure()
  plt.title(f"Column-{column}")  # Display the custom title
  plt.plot(open_f3[column])
  plt.show()

Preprocessing¶

Filtering¶

In [11]:
# Filtering
fs = 128
highcut = 30
lowcut = 0.5

def butter_bandpass(lowcut, highcut, fs, order=4):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    filter_coeffs = signal.firwin(order, [low, high], pass_zero=False)
    b = filter_coeffs
    a = 1
    return b, a

def apply_bandpass_filter(hold_f3, lowcut, highcut, fs):
    b, a = butter_bandpass(lowcut, highcut, fs)
    filtered_data = np.zeros_like(hold_f3)
    for i, column in enumerate(hold_f3.columns):
        data = hold_f3[column].values
        filtered_data[:, i] = signal.filtfilt(b, a, data)
    return pd.DataFrame(filtered_data, columns=hold_f3.columns)

# Apply the bandpass filter to all columns in the hold_f3 dataframe
hold_hand_filtered = apply_bandpass_filter(hold_f3, lowcut, highcut, fs)
filtered_hold_data = pd.DataFrame(hold_hand_filtered)
filtered_hold_data
Out[11]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 5023.618992 5142.743717 5421.730846 5223.600829 5045.839066 5132.868127 5273.596435 5178.543017 5198.911859 5302.606122 5327.912171 5215.576473 5249.524542 4939.675905 5215.576473 5055.714654 5297.050664 5116.202926 5265.572079 5207.552705
1 5012.799247 5147.671489 5415.740224 5226.422186 5046.327547 5133.846425 5269.092716 5178.137613 5197.137578 5301.976821 5334.055679 5216.163103 5246.221518 4946.834730 5211.935923 5051.561614 5308.884168 5123.357958 5257.423247 5207.242617
2 5007.389540 5156.017483 5411.752854 5226.186545 5045.264855 5137.189538 5267.957433 5178.388202 5196.573743 5301.194290 5339.660164 5211.104228 5245.921780 4947.431589 5203.359496 5049.585429 5312.688784 5123.761949 5253.330393 5208.805935
3 5007.387169 5164.520165 5406.624495 5225.410130 5043.066030 5140.567317 5269.646514 5178.608955 5201.176272 5300.146246 5341.927771 5206.160960 5248.638546 4944.800626 5189.311202 5051.915531 5309.540930 5118.628049 5252.357247 5212.376059
4 5009.196317 5171.570941 5399.850042 5225.813831 5044.569644 5142.054579 5272.233154 5177.935176 5207.014241 5299.120947 5343.760408 5208.450017 5250.061777 4945.949868 5182.306683 5056.915486 5305.962560 5116.550684 5250.429817 5214.640234
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1276 5314.486135 5269.205452 5259.381385 5320.240093 4948.925188 5228.150793 5291.283431 5298.487843 5208.462976 5183.396906 5205.769292 5359.696282 5102.018486 5033.889980 5160.256536 5263.495406 5198.129912 5223.049791 5033.064318 5017.467018
1277 5311.744003 5269.821755 5255.943661 5326.069214 4947.702614 5231.185672 5288.262899 5299.125259 5215.226410 5165.222780 5207.942905 5361.177779 5102.580670 4994.544490 5157.886887 5262.233899 5197.545834 5230.796310 5039.120125 5023.963062
1278 5309.383424 5271.501896 5252.504810 5331.888301 4947.717003 5234.527944 5285.192416 5297.048106 5217.765808 5133.927719 5207.173294 5350.762721 5106.225698 4967.450722 5157.635510 5261.582932 5200.719093 5237.914184 5044.096814 5027.427831
1279 5308.821890 5279.281614 5249.743342 5335.290896 4936.825275 5235.810022 5282.403491 5296.792523 5216.943297 5104.427652 5196.171999 5336.635813 5112.251294 4962.936506 5160.150723 5261.183226 5201.500208 5244.505536 5046.646104 5032.189478
1280 5310.629891 5287.175076 5249.524542 5334.701784 4915.604011 5237.179469 5279.768384 5300.754303 5217.428293 5092.131033 5177.925940 5328.529248 5115.585849 4969.919747 5162.494893 5260.017208 5196.442961 5250.141620 5047.690885 5041.518349

1281 rows × 20 columns

In [12]:
# Filtering Hold F3
for column in hold_hand_filtered.columns:  # Loop melalui kolom-kolom DataFrame
  plt.figure()
  plt.title(f"Column-{column}")  # Display the custom title
  plt.plot(hold_hand_filtered[column])
  plt.show()
In [13]:
# Filtering Open F3
def apply_bandpass_filter(open_f3, lowcut, highcut, fs):
    b, a = butter_bandpass(lowcut, highcut, fs)
    filtered_data = np.zeros_like(open_f3)
    for i, column in enumerate(open_f3.columns):
        data = open_f3[column].values
        filtered_data[:, i] = signal.filtfilt(b, a, data)
    return pd.DataFrame(filtered_data, columns=open_f3.columns)

# Apply the bandpass filter to all columns in the open_f3 dataframe
open_hand_filtered = apply_bandpass_filter(open_f3, lowcut, highcut, fs)
filtered_open_data = pd.DataFrame(open_hand_filtered)
filtered_open_data
Out[13]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 5147.064433 5161.260738 5352.601141 5542.090312 4834.129821 4986.584949 5197.060038 5181.011914 5185.949708 5105.093183 5210.021601 5264.337924 5024.853147 4794.010096 5084.107264 5088.427981 5217.428293 5163.112557 5155.088201 5293.347611
1 5148.089586 5163.650667 5354.472827 5538.317615 4816.714627 4989.886655 5198.865616 5208.277835 5182.154873 5106.473677 5213.829642 5267.864980 5036.114258 4798.351880 5090.490744 5077.506380 5214.610414 5173.878939 5160.543050 5298.484384
2 5146.619641 5160.866706 5354.921359 5539.170933 4798.815721 4992.068594 5203.615789 5225.233237 5182.755322 5111.369175 5215.394051 5263.797560 5047.209138 4790.310033 5095.564855 5068.501447 5215.016784 5176.835925 5164.543993 5300.604674
3 5143.639789 5146.062225 5357.153873 5542.153657 4797.892190 4993.324624 5207.487819 5226.385609 5185.835268 5117.475422 5218.384800 5255.057294 5056.297065 4748.973255 5097.345095 5055.435471 5218.027234 5173.882389 5163.498098 5302.534541
4 5143.960741 5129.010091 5360.169202 5544.135024 4812.090146 4993.855777 5208.805961 5221.599125 5187.612733 5120.238981 5223.532262 5249.579247 5064.980501 4693.860335 5096.437203 5035.557628 5220.989372 5170.246848 5159.747514 5303.549756
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1276 5207.054987 5164.137259 5270.059400 5177.556473 5057.062921 5433.657457 5269.916844 5232.280583 5325.142818 5264.575809 5299.836932 4123.181459 5247.264477 5028.259352 5205.145302 5003.286911 5209.748971 5270.366066 5230.687288 5123.681362
1277 5212.045812 5161.522325 5269.257786 5152.961884 5056.994267 5433.534399 5271.537349 5233.614722 5330.331531 5261.879194 5303.759154 4133.225410 5246.276906 5030.991632 5200.275997 5005.130349 5204.497341 5273.774586 5230.417431 5121.056499
1278 5211.620138 5163.763548 5269.975941 5138.082675 5059.364676 5436.087035 5270.767761 5234.595889 5331.327631 5263.161752 5304.773370 4146.415666 5245.371422 5035.305113 5197.034383 5007.503198 5195.545274 5277.495573 5230.189655 5119.275095
1279 5209.791732 5162.893587 5271.746234 5139.669357 5059.326994 5436.952074 5265.742424 5237.732045 5327.035029 5265.166394 5305.715128 4156.406534 5244.690036 5037.376386 5197.317885 5011.100029 5184.734742 5281.534283 5227.464547 5119.194976
1280 5208.787447 5157.557099 5273.596435 5147.681511 5056.331732 5435.310074 5260.634285 5240.266031 5322.974377 5264.337924 5306.926252 4162.585708 5245.203825 5037.815297 5200.763091 5018.063533 5170.519249 5282.854359 5221.131932 5119.906565

1281 rows × 20 columns

In [14]:
# Filtering Open F3
for column in open_hand_filtered.columns:  # Loop melalui kolom-kolom DataFrame
  plt.figure()
  plt.title(f"Column-{column}")  # Display the custom title
  plt.plot(open_hand_filtered[column])
  plt.show()

Fast Fourier Transform¶

In [15]:
# Compute FFT for each column in hold_f3_filtered dataframe
fft_data = np.fft.fft(hold_hand_filtered.values, axis=0)
fft_freq = np.fft.fftfreq(hold_hand_filtered.shape[0], 1/fs)
fft_hold_df = pd.DataFrame(columns=hold_hand_filtered.columns, index=fft_freq[:hold_hand_filtered.shape[0]//2])

# Plot FFT for each column
for i, column in enumerate(hold_hand_filtered.columns):
  fig, ax=plt.subplots(figsize=(8,6))
  fft_hold_df[column] = np.abs(fft_data[:hold_hand_filtered.shape[0]//2, i])
  ax.plot(fft_hold_df[column])
  ax.set_xlabel('Frequency (Hz)')
  ax.set_ylabel('Magnitude')
  ax.set_xlim([0,65])
  ax.set_ylim([0,50000])
  plt.show()

fft_hold_df
fft_hold_indexing = fft_hold_df.reset_index(drop = True)
fft_hold_indexing
Out[15]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 6.636871e+06 6.738258e+06 6.747208e+06 6.769092e+06 6.322167e+06 6.449325e+06 6.629144e+06 6.637523e+06 6.695402e+06 6.673422e+06 6.756949e+06 6.676797e+06 6.688475e+06 6.458320e+06 6.532971e+06 6.684201e+06 6.691187e+06 6.671684e+06 6.755979e+06 6.694968e+06
1 2.912420e+04 3.499883e+04 6.864195e+04 4.239030e+04 1.373386e+04 8.598626e+04 2.257963e+04 4.135451e+04 4.471730e+04 1.376212e+04 2.552434e+04 3.251306e+04 3.361578e+04 8.103503e+03 3.641821e+04 3.776819e+04 6.910825e+04 2.694972e+04 5.443365e+04 3.423020e+04
2 4.938947e+04 3.742747e+04 1.424622e+04 4.769630e+04 8.348167e+03 6.410478e+04 5.159508e+04 3.958217e+04 9.266842e+03 3.724818e+04 1.463693e+04 1.281266e+04 9.203949e+03 4.205576e+04 3.454984e+04 3.557810e+03 2.028223e+04 1.339374e+04 2.673127e+04 1.447533e+04
3 1.914127e+04 8.933757e+04 3.624079e+04 1.475893e+04 2.970978e+03 1.261235e+04 3.755111e+04 3.833346e+04 2.294031e+04 3.431509e+04 1.888790e+04 2.004339e+04 2.422466e+04 1.578900e+04 1.002371e+04 3.423227e+04 4.776111e+04 1.201863e+04 1.847734e+04 3.889765e+04
4 2.020634e+04 1.054444e+04 2.440861e+04 1.780810e+04 1.780697e+04 3.294964e+04 1.975547e+04 2.886908e+04 2.866454e+03 3.025963e+04 1.220568e+04 1.977023e+03 3.248081e+03 1.964703e+04 1.923526e+04 2.547864e+04 7.855791e+03 1.195007e+04 1.332843e+04 2.802802e+04
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
635 1.410990e+02 7.444429e+01 8.776465e+01 5.604191e+01 7.083062e+01 5.264935e+01 1.019176e+00 6.237307e+01 9.501615e+00 1.064234e+02 7.864788e+01 5.621858e+01 6.766079e+01 1.973572e+01 2.694907e+01 1.006660e+02 4.849300e+01 7.055902e+01 1.111358e+02 8.038327e+01
636 1.410943e+02 7.444173e+01 8.776233e+01 5.603944e+01 7.082867e+01 5.264759e+01 1.018942e+00 6.237055e+01 9.500061e+00 1.064197e+02 7.864665e+01 5.621608e+01 6.765816e+01 1.973521e+01 2.694611e+01 1.006631e+02 4.849191e+01 7.055713e+01 1.111314e+02 8.038195e+01
637 1.410903e+02 7.444058e+01 8.775989e+01 5.603882e+01 7.082679e+01 5.264598e+01 1.019602e+00 6.236905e+01 9.499915e+00 1.064177e+02 7.864494e+01 5.621500e+01 6.765640e+01 1.973449e+01 2.694634e+01 1.006609e+02 4.849028e+01 7.055573e+01 1.111291e+02 8.037938e+01
638 1.410881e+02 7.443896e+01 8.775872e+01 5.603807e+01 7.082521e+01 5.264512e+01 1.019091e+00 6.236858e+01 9.499573e+00 1.064157e+02 7.864295e+01 5.621440e+01 6.765542e+01 1.973452e+01 2.694542e+01 1.006587e+02 4.848914e+01 7.055427e+01 1.111269e+02 8.037835e+01
639 1.410863e+02 7.443811e+01 8.775766e+01 5.603733e+01 7.082427e+01 5.264467e+01 1.019091e+00 6.236770e+01 9.499381e+00 1.064146e+02 7.864207e+01 5.621337e+01 6.765444e+01 1.973403e+01 2.694500e+01 1.006576e+02 4.848862e+01 7.055350e+01 1.111256e+02 8.037722e+01

640 rows × 20 columns

In [16]:
# Compute FFT for each column in open_f3_filtered dataframe
fft_data = np.fft.fft(open_hand_filtered.values, axis=0)
fft_freq = np.fft.fftfreq(open_hand_filtered.shape[0], 1/fs)
fft_open_df = pd.DataFrame(columns=open_hand_filtered.columns, index=fft_freq[:open_hand_filtered.shape[0]//2])

# Plot FFT for each column
for i, column in enumerate(open_hand_filtered.columns):
  fig, ax=plt.subplots(figsize=(8,6))
  fft_open_df[column] = np.abs(fft_data[:open_hand_filtered.shape[0]//2, i])
  ax.plot(fft_open_df[column])
  ax.set_xlabel('Frequency (Hz)')
  ax.set_ylabel('Magnitude')
  ax.set_xlim([0,65])
  ax.set_ylim([0,50000])
  plt.show()

fft_open_df
fft_open_indexing = fft_open_df.reset_index(drop = True)
fft_open_indexing
Out[16]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 6.685101e+06 6.673000e+06 6.703298e+06 6.750850e+06 6.354045e+06 6.685276e+06 6.619522e+06 6.641167e+06 6.749840e+06 6.677142e+06 6.751121e+06 6.610367e+06 6.712154e+06 6.519289e+06 6.574786e+06 6.586368e+06 6.643646e+06 6.693521e+06 6.699553e+06 6.762855e+06
1 4.866684e+04 6.130269e+04 2.165192e+04 2.840652e+04 7.884178e+04 5.328847e+04 1.057832e+05 5.400525e+04 6.731749e+04 4.656792e+04 6.256182e+04 1.891853e+04 3.393802e+04 3.293704e+04 4.195691e+04 6.917589e+04 2.236794e+04 3.564499e+04 3.531990e+04 2.959156e+04
2 3.233926e+04 3.075478e+04 8.207080e+03 1.164771e+03 2.875108e+04 3.707064e+04 2.123084e+04 2.854249e+04 4.979736e+04 2.398951e+04 2.097497e+04 1.023772e+05 4.096156e+04 8.374310e+04 2.159530e+04 3.820125e+04 1.145969e+04 1.458225e+04 2.482035e+04 2.416521e+04
3 2.777043e+04 9.784667e+03 4.282082e+04 2.053382e+04 9.203555e+04 4.415848e+04 1.778644e+04 3.887262e+04 2.871289e+04 2.181220e+04 2.966341e+04 5.471631e+04 4.428728e+04 3.808468e+03 3.062262e+04 2.751657e+04 1.502005e+04 1.661913e+04 1.736718e+04 2.930911e+04
4 2.917422e+04 4.670411e+03 2.539113e+04 1.548108e+04 2.888602e+04 4.337548e+04 4.103246e+04 2.345691e+04 3.757567e+04 3.179966e+04 1.433362e+04 3.266775e+04 5.501900e+04 3.436169e+04 1.795078e+04 3.081883e+04 1.478372e+04 1.028837e+04 4.377318e+03 4.494690e+04
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
635 3.110130e+01 2.834611e+00 3.824271e+01 1.966943e+02 1.067806e+02 2.247158e+02 3.076986e+01 3.730843e+01 6.639802e+01 7.875760e+01 5.015070e+01 5.480111e+02 1.131264e+02 1.234023e+02 6.103679e+01 3.612312e+01 2.845105e+01 6.318641e+01 3.239995e+01 8.475028e+01
636 3.109993e+01 2.832988e+00 3.824035e+01 1.966896e+02 1.067787e+02 2.247088e+02 3.076902e+01 3.730842e+01 6.639504e+01 7.875704e+01 5.014813e+01 5.479947e+02 1.131231e+02 1.233974e+02 6.103566e+01 3.612342e+01 2.844935e+01 6.318439e+01 3.240045e+01 8.474759e+01
637 3.109897e+01 2.833042e+00 3.823864e+01 1.966841e+02 1.067752e+02 2.247040e+02 3.076820e+01 3.730727e+01 6.639364e+01 7.875400e+01 5.014734e+01 5.479817e+02 1.131208e+02 1.233932e+02 6.103427e+01 3.612302e+01 2.844870e+01 6.318254e+01 3.239973e+01 8.474663e+01
638 3.109901e+01 2.832834e+00 3.823811e+01 1.966803e+02 1.067731e+02 2.246997e+02 3.076773e+01 3.730629e+01 6.639275e+01 7.875286e+01 5.014614e+01 5.479723e+02 1.131181e+02 1.233903e+02 6.103335e+01 3.612138e+01 2.844802e+01 6.318177e+01 3.239834e+01 8.474455e+01
639 3.109851e+01 2.832843e+00 3.823767e+01 1.966780e+02 1.067718e+02 2.246971e+02 3.076714e+01 3.730605e+01 6.639179e+01 7.875212e+01 5.014545e+01 5.479655e+02 1.131169e+02 1.233881e+02 6.103271e+01 3.612137e+01 2.844777e+01 6.318080e+01 3.239801e+01 8.474352e+01

640 rows × 20 columns

Feature Extraction¶

Power Spectral Density¶

In [17]:
# Power Spectral Density Hold
def extract_band_power(fft_hold_indexing, fs, band, level):
    # Create an empty dictionary to hold the band powers for each column
    band_powers = {}
    
    # Loop over all columns in the DataFrame
    for column in fft_hold_indexing.columns:
        # Apply wavelet decomposition with specified level
        coeffs = wavedec(fft_hold_indexing[column], 'db4', level=level)

        # Get the approximate coefficients of the specified level
        cA = coeffs[level]

        # Compute power spectral density (PSD) using Welch's method
        f, Pxx = welch(cA, fs, nperseg=min(256, len(cA)))

        # Define frequency bands
        freq_bands = {'delta': [0.5, 4],
                      'theta': [4, 8],
                      'alpha': [8, 13],
                      'beta': [13, 30]}

        # Define frequency indices
        freq_ix = {'delta': np.logical_and(f >= freq_bands['delta'][0], f < freq_bands['delta'][1]),
                   'theta': np.logical_and(f >= freq_bands['theta'][0], f < freq_bands['theta'][1]),
                   'alpha': np.logical_and(f >= freq_bands['alpha'][0], f < freq_bands['alpha'][1]),
                   'beta': np.logical_and(f >= freq_bands['beta'][0], f < freq_bands['beta'][1])}

        # Calculate power in the specified band
        mask = freq_ix[band]
        band_power = np.trapz(y=Pxx[mask], x=f[mask])

        # Add the band power to the dictionary for this column
        band_powers[column] = band_power
    
    # Create a DataFrame from the band powers dictionary and return it
    return pd.DataFrame.from_dict(band_powers, orient='index', columns=[band])

# Define the sampling frequency and wavelet decomposition level
fs = 128
level = 4

# Define the input DataFrame with 20 columns of hold hand F3 data
# Replace this with your own DataFrame
fft_hold_indexing = pd.DataFrame(np.random.randn(1000, 20))

# Loop over all frequency bands and compute the band powers for each column
band_power_hold = pd.DataFrame()
for band in ['delta', 'theta', 'alpha', 'beta']:
    band_power = extract_band_power(fft_hold_indexing, fs, band, level)
    band_power_hold = pd.concat([band_power_hold, band_power], axis=1)

band_power_hold
Out[17]:
delta theta alpha beta
0 0.034556 0.076501 0.067483 0.230142
1 0.046539 0.049498 0.048339 0.186967
2 0.077010 0.057129 0.073475 0.220910
3 0.052009 0.065631 0.062209 0.195104
4 0.062595 0.048581 0.043320 0.323552
5 0.041028 0.056193 0.061224 0.309484
6 0.051230 0.040134 0.049657 0.269233
7 0.049822 0.050272 0.075331 0.309986
8 0.066299 0.048886 0.050576 0.287538
9 0.042296 0.093588 0.070265 0.256152
10 0.017390 0.070242 0.071162 0.357277
11 0.036992 0.026919 0.102285 0.278286
12 0.050425 0.068950 0.045441 0.287481
13 0.045168 0.032941 0.056652 0.267882
14 0.037876 0.050107 0.081672 0.350718
15 0.052029 0.051347 0.048946 0.247716
16 0.093829 0.028002 0.047603 0.331691
17 0.060638 0.070508 0.048212 0.220416
18 0.050627 0.059317 0.062637 0.260917
19 0.040528 0.053551 0.071544 0.298648
In [18]:
# Power Spectral Density Open
def extract_band_power(fft_open_indexing, fs, band, level):
    # Create an empty dictionary to open the band powers for each column
    band_powers = {}
    
    # Loop over all columns in the DataFrame
    for column in fft_open_indexing.columns:
        # Apply wavelet decomposition with specified level
        coeffs = wavedec(fft_open_indexing[column], 'db4', level=level)

        # Get the approximate coefficients of the specified level
        cA = coeffs[level]

        # Compute power spectral density (PSD) using Welch's method
        f, Pxx = welch(cA, fs, nperseg=min(256, len(cA)))

        # Define frequency bands
        freq_bands = {'delta': [0.5, 4],
                      'theta': [4, 8],
                      'alpha': [8, 13],
                      'beta': [13, 30]}

        # Define frequency indices
        freq_ix = {'delta': np.logical_and(f >= freq_bands['delta'][0], f < freq_bands['delta'][1]),
                   'theta': np.logical_and(f >= freq_bands['theta'][0], f < freq_bands['theta'][1]),
                   'alpha': np.logical_and(f >= freq_bands['alpha'][0], f < freq_bands['alpha'][1]),
                   'beta': np.logical_and(f >= freq_bands['beta'][0], f < freq_bands['beta'][1])}

        # Calculate power in the specified band
        mask = freq_ix[band]
        band_power = np.trapz(y=Pxx[mask], x=f[mask])

        # Add the band power to the dictionary for this column
        band_powers[column] = band_power
    
    # Create a DataFrame from the band powers dictionary and return it
    return pd.DataFrame.from_dict(band_powers, orient='index', columns=[band])

# Define the sampling frequency and wavelet decomposition level
fs = 128
level = 4

# Define the input DataFrame with 20 columns of open hand F3 data
# Replace this with your own DataFrame
fft_open_indexing = pd.DataFrame(np.random.randn(1000, 20))

# Loop over all frequency bands and compute the band powers for each column
band_power_open = pd.DataFrame()
for band in ['delta','theta','alpha','beta']:
    band_power = extract_band_power(fft_open_indexing, fs, band, level)
    band_power_open= pd.concat([band_power_open, band_power], axis=1)

band_power_open
Out[18]:
delta theta alpha beta
0 0.045657 0.052829 0.113011 0.278737
1 0.073855 0.095474 0.077504 0.259815
2 0.047561 0.064578 0.069451 0.330307
3 0.059468 0.047405 0.051626 0.285080
4 0.032684 0.038351 0.048928 0.245009
5 0.043561 0.072305 0.068141 0.230281
6 0.082703 0.070086 0.052686 0.259997
7 0.034529 0.059071 0.022520 0.235574
8 0.042204 0.025418 0.066486 0.274210
9 0.064815 0.039218 0.099146 0.279214
10 0.028852 0.035649 0.078844 0.292869
11 0.034370 0.066801 0.047832 0.247040
12 0.044788 0.036548 0.066373 0.329328
13 0.090509 0.075467 0.094019 0.186177
14 0.039060 0.040785 0.084241 0.242107
15 0.027008 0.094852 0.063980 0.262093
16 0.051717 0.107500 0.058318 0.366065
17 0.059056 0.105731 0.065285 0.316432
18 0.045102 0.073012 0.060312 0.284514
19 0.046701 0.062353 0.070722 0.248787

Entropy¶

In [19]:
from scipy.stats import entropy
entropy_hold = np.apply_along_axis(entropy,1,abs(band_power_hold))
entropy_hold_df = pd.DataFrame(entropy_hold, columns=['entropy'])

entropy_hold_df
Out[19]:
entropy
0 1.143313
1 1.163421
2 1.221020
3 1.217001
4 0.980342
5 1.007474
6 1.019207
7 1.044032
8 1.054774
9 1.155655
10 0.913472
11 1.007992
12 1.050234
13 0.997259
14 0.972643
15 1.082628
16 0.971616
17 1.175462
18 1.108041
19 1.033990
In [20]:
from scipy.stats import entropy
entropy_open = np.apply_along_axis(entropy,1,abs(band_power_open))
entropy_open_df = pd.DataFrame(entropy_open, columns=['entropy'])

entropy_open_df
Out[20]:
entropy
0 1.120447
1 1.224905
2 1.035635
3 1.042828
4 0.989755
5 1.164807
6 1.163968
7 0.971924
8 0.970355
9 1.115388
10 0.960992
11 1.062028
12 0.949151
13 1.317020
14 1.090651
15 1.089606
16 1.049094
17 1.128456
18 1.082873
19 1.135034
In [21]:
df_hold = pd.concat([band_power_hold, entropy_hold_df], axis=1)
df_hold
Out[21]:
delta theta alpha beta entropy
0 0.034556 0.076501 0.067483 0.230142 1.143313
1 0.046539 0.049498 0.048339 0.186967 1.163421
2 0.077010 0.057129 0.073475 0.220910 1.221020
3 0.052009 0.065631 0.062209 0.195104 1.217001
4 0.062595 0.048581 0.043320 0.323552 0.980342
5 0.041028 0.056193 0.061224 0.309484 1.007474
6 0.051230 0.040134 0.049657 0.269233 1.019207
7 0.049822 0.050272 0.075331 0.309986 1.044032
8 0.066299 0.048886 0.050576 0.287538 1.054774
9 0.042296 0.093588 0.070265 0.256152 1.155655
10 0.017390 0.070242 0.071162 0.357277 0.913472
11 0.036992 0.026919 0.102285 0.278286 1.007992
12 0.050425 0.068950 0.045441 0.287481 1.050234
13 0.045168 0.032941 0.056652 0.267882 0.997259
14 0.037876 0.050107 0.081672 0.350718 0.972643
15 0.052029 0.051347 0.048946 0.247716 1.082628
16 0.093829 0.028002 0.047603 0.331691 0.971616
17 0.060638 0.070508 0.048212 0.220416 1.175462
18 0.050627 0.059317 0.062637 0.260917 1.108041
19 0.040528 0.053551 0.071544 0.298648 1.033990
In [22]:
df_open = pd.concat([band_power_open,entropy_open_df], axis=1)
df_open
Out[22]:
delta theta alpha beta entropy
0 0.045657 0.052829 0.113011 0.278737 1.120447
1 0.073855 0.095474 0.077504 0.259815 1.224905
2 0.047561 0.064578 0.069451 0.330307 1.035635
3 0.059468 0.047405 0.051626 0.285080 1.042828
4 0.032684 0.038351 0.048928 0.245009 0.989755
5 0.043561 0.072305 0.068141 0.230281 1.164807
6 0.082703 0.070086 0.052686 0.259997 1.163968
7 0.034529 0.059071 0.022520 0.235574 0.971924
8 0.042204 0.025418 0.066486 0.274210 0.970355
9 0.064815 0.039218 0.099146 0.279214 1.115388
10 0.028852 0.035649 0.078844 0.292869 0.960992
11 0.034370 0.066801 0.047832 0.247040 1.062028
12 0.044788 0.036548 0.066373 0.329328 0.949151
13 0.090509 0.075467 0.094019 0.186177 1.317020
14 0.039060 0.040785 0.084241 0.242107 1.090651
15 0.027008 0.094852 0.063980 0.262093 1.089606
16 0.051717 0.107500 0.058318 0.366065 1.049094
17 0.059056 0.105731 0.065285 0.316432 1.128456
18 0.045102 0.073012 0.060312 0.284514 1.082873
19 0.046701 0.062353 0.070722 0.248787 1.135034

Statistical Feature¶

In [23]:
# Mencari mean hold
mean_hold = df_hold.mean(axis=1)
df_mean_hold = pd.DataFrame(mean_hold, columns=['mean'])
df_mean_hold
Out[23]:
mean
0 0.310399
1 0.298953
2 0.329909
3 0.318391
4 0.291678
5 0.295081
6 0.285892
7 0.305889
8 0.301615
9 0.323591
10 0.285909
11 0.290495
12 0.300506
13 0.279980
14 0.298603
15 0.296533
16 0.294548
17 0.315047
18 0.308308
19 0.299652
In [24]:
# Mencari mean open
mean_open = df_open.mean(axis=1)
df_mean_open = pd.DataFrame(mean_open, columns=['mean'])
df_mean_open
Out[24]:
mean
0 0.322136
1 0.346310
2 0.309506
3 0.297282
4 0.270946
5 0.315819
6 0.325888
7 0.264723
8 0.275735
9 0.319556
10 0.279441
11 0.291614
12 0.285238
13 0.352638
14 0.299369
15 0.307508
16 0.326539
17 0.334992
18 0.309163
19 0.312720
In [25]:
# Mencari variance hold
var_hold = df_hold.var(axis=1)
df_var_hold = pd.DataFrame(var_hold, columns=['var'])
df_var_hold
Out[25]:
var
0 0.222498
1 0.237148
2 0.252521
3 0.255794
4 0.162133
5 0.171003
6 0.177325
7 0.182235
8 0.187428
9 0.223270
10 0.140915
11 0.171028
12 0.185870
13 0.170169
14 0.158459
15 0.200381
16 0.158029
17 0.236248
18 0.207642
19 0.179749
In [26]:
#Mencari variance open
var_open = df_open.var(axis=1)
df_var_open = pd.DataFrame(var_open, columns=['var'])
df_var_open
Out[26]:
var
0 0.207971
1 0.247204
2 0.178482
3 0.183832
4 0.169380
5 0.230716
6 0.226483
7 0.163732
8 0.160870
9 0.206760
10 0.156790
11 0.192917
12 0.152572
13 0.292540
14 0.202578
15 0.199237
16 0.179774
17 0.207843
18 0.196664
19 0.218075
In [27]:
# Mencari std hold
std_hold = df_hold.std(axis=1)
df_std_hold = pd.DataFrame(std_hold, columns=['std'])
df_std_hold
Out[27]:
std
0 0.471697
1 0.486979
2 0.502515
3 0.505761
4 0.402657
5 0.413525
6 0.421099
7 0.426890
8 0.432930
9 0.472515
10 0.375386
11 0.413555
12 0.431126
13 0.412515
14 0.398068
15 0.447640
16 0.397529
17 0.486054
18 0.455678
19 0.423969
In [28]:
#Mencari std open
std_open = df_open.std(axis=1)
df_std_open = pd.DataFrame(std_open, columns=['std'])
df_std_open
Out[28]:
std
0 0.456038
1 0.497196
2 0.422471
3 0.428757
4 0.411558
5 0.480329
6 0.475902
7 0.404638
8 0.401086
9 0.454708
10 0.395967
11 0.439224
12 0.390605
13 0.540870
14 0.450087
15 0.446360
16 0.423997
17 0.455898
18 0.443468
19 0.466985
In [29]:
# Mencari min hold
min_hold = df_hold.min(axis=1)
df_min_hold = pd.DataFrame(min_hold, columns=['min'])
df_min_hold
Out[29]:
min
0 0.034556
1 0.046539
2 0.057129
3 0.052009
4 0.043320
5 0.041028
6 0.040134
7 0.049822
8 0.048886
9 0.042296
10 0.017390
11 0.026919
12 0.045441
13 0.032941
14 0.037876
15 0.048946
16 0.028002
17 0.048212
18 0.050627
19 0.040528
In [30]:
# Mencari min open
min_open = df_open.min(axis=1)
df_min_open = pd.DataFrame(min_open, columns=['min'])
df_min_open
Out[30]:
min
0 0.045657
1 0.073855
2 0.047561
3 0.047405
4 0.032684
5 0.043561
6 0.052686
7 0.022520
8 0.025418
9 0.039218
10 0.028852
11 0.034370
12 0.036548
13 0.075467
14 0.039060
15 0.027008
16 0.051717
17 0.059056
18 0.045102
19 0.046701
In [31]:
#Mencari max hold
max_hold = df_hold.max(axis=1)
df_max_hold = pd.DataFrame(max_hold, columns=['max'])
df_max_hold
Out[31]:
max
0 1.143313
1 1.163421
2 1.221020
3 1.217001
4 0.980342
5 1.007474
6 1.019207
7 1.044032
8 1.054774
9 1.155655
10 0.913472
11 1.007992
12 1.050234
13 0.997259
14 0.972643
15 1.082628
16 0.971616
17 1.175462
18 1.108041
19 1.033990
In [32]:
#Mencari max open
max_open = df_open.max(axis=1)
df_max_open = pd.DataFrame(max_open, columns=['max'])
df_max_open
Out[32]:
max
0 1.120447
1 1.224905
2 1.035635
3 1.042828
4 0.989755
5 1.164807
6 1.163968
7 0.971924
8 0.970355
9 1.115388
10 0.960992
11 1.062028
12 0.949151
13 1.317020
14 1.090651
15 1.089606
16 1.049094
17 1.128456
18 1.082873
19 1.135034
In [33]:
#Membuat dataframe baru dari feature yang baru
df_feat_hold = pd.concat([df_mean_hold, df_std_hold, df_var_hold, df_min_hold, df_max_hold], axis=1)
df_feat_hold
Out[33]:
mean std var min max
0 0.310399 0.471697 0.222498 0.034556 1.143313
1 0.298953 0.486979 0.237148 0.046539 1.163421
2 0.329909 0.502515 0.252521 0.057129 1.221020
3 0.318391 0.505761 0.255794 0.052009 1.217001
4 0.291678 0.402657 0.162133 0.043320 0.980342
5 0.295081 0.413525 0.171003 0.041028 1.007474
6 0.285892 0.421099 0.177325 0.040134 1.019207
7 0.305889 0.426890 0.182235 0.049822 1.044032
8 0.301615 0.432930 0.187428 0.048886 1.054774
9 0.323591 0.472515 0.223270 0.042296 1.155655
10 0.285909 0.375386 0.140915 0.017390 0.913472
11 0.290495 0.413555 0.171028 0.026919 1.007992
12 0.300506 0.431126 0.185870 0.045441 1.050234
13 0.279980 0.412515 0.170169 0.032941 0.997259
14 0.298603 0.398068 0.158459 0.037876 0.972643
15 0.296533 0.447640 0.200381 0.048946 1.082628
16 0.294548 0.397529 0.158029 0.028002 0.971616
17 0.315047 0.486054 0.236248 0.048212 1.175462
18 0.308308 0.455678 0.207642 0.050627 1.108041
19 0.299652 0.423969 0.179749 0.040528 1.033990
In [34]:
#Membuat dataframe baru dari feature yang baru
df_feat_open = pd.concat([df_mean_open, df_std_open, df_var_open, df_min_open, df_max_open], axis=1)
df_feat_open
Out[34]:
mean std var min max
0 0.322136 0.456038 0.207971 0.045657 1.120447
1 0.346310 0.497196 0.247204 0.073855 1.224905
2 0.309506 0.422471 0.178482 0.047561 1.035635
3 0.297282 0.428757 0.183832 0.047405 1.042828
4 0.270946 0.411558 0.169380 0.032684 0.989755
5 0.315819 0.480329 0.230716 0.043561 1.164807
6 0.325888 0.475902 0.226483 0.052686 1.163968
7 0.264723 0.404638 0.163732 0.022520 0.971924
8 0.275735 0.401086 0.160870 0.025418 0.970355
9 0.319556 0.454708 0.206760 0.039218 1.115388
10 0.279441 0.395967 0.156790 0.028852 0.960992
11 0.291614 0.439224 0.192917 0.034370 1.062028
12 0.285238 0.390605 0.152572 0.036548 0.949151
13 0.352638 0.540870 0.292540 0.075467 1.317020
14 0.299369 0.450087 0.202578 0.039060 1.090651
15 0.307508 0.446360 0.199237 0.027008 1.089606
16 0.326539 0.423997 0.179774 0.051717 1.049094
17 0.334992 0.455898 0.207843 0.059056 1.128456
18 0.309163 0.443468 0.196664 0.045102 1.082873
19 0.312720 0.466985 0.218075 0.046701 1.135034
In [35]:
df_combi_hold = pd.concat([df_hold, df_feat_hold], axis=1)
df_combi_hold
Out[35]:
delta theta alpha beta entropy mean std var min max
0 0.034556 0.076501 0.067483 0.230142 1.143313 0.310399 0.471697 0.222498 0.034556 1.143313
1 0.046539 0.049498 0.048339 0.186967 1.163421 0.298953 0.486979 0.237148 0.046539 1.163421
2 0.077010 0.057129 0.073475 0.220910 1.221020 0.329909 0.502515 0.252521 0.057129 1.221020
3 0.052009 0.065631 0.062209 0.195104 1.217001 0.318391 0.505761 0.255794 0.052009 1.217001
4 0.062595 0.048581 0.043320 0.323552 0.980342 0.291678 0.402657 0.162133 0.043320 0.980342
5 0.041028 0.056193 0.061224 0.309484 1.007474 0.295081 0.413525 0.171003 0.041028 1.007474
6 0.051230 0.040134 0.049657 0.269233 1.019207 0.285892 0.421099 0.177325 0.040134 1.019207
7 0.049822 0.050272 0.075331 0.309986 1.044032 0.305889 0.426890 0.182235 0.049822 1.044032
8 0.066299 0.048886 0.050576 0.287538 1.054774 0.301615 0.432930 0.187428 0.048886 1.054774
9 0.042296 0.093588 0.070265 0.256152 1.155655 0.323591 0.472515 0.223270 0.042296 1.155655
10 0.017390 0.070242 0.071162 0.357277 0.913472 0.285909 0.375386 0.140915 0.017390 0.913472
11 0.036992 0.026919 0.102285 0.278286 1.007992 0.290495 0.413555 0.171028 0.026919 1.007992
12 0.050425 0.068950 0.045441 0.287481 1.050234 0.300506 0.431126 0.185870 0.045441 1.050234
13 0.045168 0.032941 0.056652 0.267882 0.997259 0.279980 0.412515 0.170169 0.032941 0.997259
14 0.037876 0.050107 0.081672 0.350718 0.972643 0.298603 0.398068 0.158459 0.037876 0.972643
15 0.052029 0.051347 0.048946 0.247716 1.082628 0.296533 0.447640 0.200381 0.048946 1.082628
16 0.093829 0.028002 0.047603 0.331691 0.971616 0.294548 0.397529 0.158029 0.028002 0.971616
17 0.060638 0.070508 0.048212 0.220416 1.175462 0.315047 0.486054 0.236248 0.048212 1.175462
18 0.050627 0.059317 0.062637 0.260917 1.108041 0.308308 0.455678 0.207642 0.050627 1.108041
19 0.040528 0.053551 0.071544 0.298648 1.033990 0.299652 0.423969 0.179749 0.040528 1.033990

Normalization Feature¶

In [36]:
from sklearn.preprocessing import MinMaxScaler

# Create an instance of the MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))

# Normalize the features in the DataFrame
normalized_df_combi_hold = pd.DataFrame(scaler.fit_transform(df_combi_hold), columns=df_combi_hold.columns)
normalized_df_combi_hold
Out[36]:
delta theta alpha beta entropy mean std var min max
0 0.224576 0.743703 0.409782 0.253505 0.747335 0.609249 0.738726 0.710168 0.431980 0.747335
1 0.381333 0.338677 0.085124 0.000000 0.812714 0.379995 0.855938 0.837693 0.733507 0.812714
2 0.779967 0.453134 0.511401 0.199298 1.000000 1.000000 0.975105 0.971513 1.000000 1.000000
3 0.452898 0.580668 0.320354 0.047774 0.986933 0.769318 1.000000 1.000000 0.871165 0.986933
4 0.591392 0.324916 0.000000 0.801978 0.217430 0.234292 0.209173 0.184698 0.652508 0.217430
5 0.309246 0.439091 0.303634 0.719378 0.305651 0.302442 0.292530 0.261909 0.594845 0.305651
6 0.442711 0.198220 0.107479 0.483037 0.343801 0.118414 0.350628 0.316940 0.572332 0.343801
7 0.424288 0.350285 0.542886 0.722320 0.424520 0.518912 0.395042 0.359682 0.816133 0.424520
8 0.639851 0.329494 0.123062 0.590514 0.459445 0.433308 0.441368 0.404887 0.792570 0.459445
9 0.325833 1.000000 0.456968 0.406228 0.787465 0.873475 0.744997 0.716888 0.626752 0.787465
10 0.000000 0.649820 0.472184 1.000000 0.000000 0.118739 0.000000 0.000000 0.000000 0.000000
11 0.256445 0.000000 1.000000 0.536192 0.307335 0.210594 0.292761 0.262125 0.239779 0.307335
12 0.432175 0.630441 0.035964 0.590179 0.444685 0.411104 0.427536 0.391323 0.705872 0.444685
13 0.363396 0.090332 0.226108 0.475100 0.272434 0.000000 0.284786 0.254649 0.391329 0.272434
14 0.268006 0.347817 0.650422 0.961489 0.192397 0.372999 0.173976 0.152713 0.515518 0.192397
15 0.453162 0.366415 0.095409 0.356693 0.550014 0.331533 0.554199 0.517644 0.794077 0.550014
16 1.000000 0.016246 0.072636 0.849767 0.189056 0.291775 0.169839 0.148977 0.267036 0.189056
17 0.565782 0.653814 0.082959 0.196399 0.851867 0.702344 0.848844 0.829860 0.775603 0.851867
18 0.434821 0.485950 0.327598 0.434209 0.632647 0.567366 0.615854 0.580850 0.836392 0.632647
19 0.302703 0.399471 0.478666 0.655747 0.391868 0.394007 0.372636 0.338047 0.582260 0.391868
In [37]:
df_combi_open = pd.concat([df_open, df_feat_open], axis=1)
df_combi_open
Out[37]:
delta theta alpha beta entropy mean std var min max
0 0.045657 0.052829 0.113011 0.278737 1.120447 0.322136 0.456038 0.207971 0.045657 1.120447
1 0.073855 0.095474 0.077504 0.259815 1.224905 0.346310 0.497196 0.247204 0.073855 1.224905
2 0.047561 0.064578 0.069451 0.330307 1.035635 0.309506 0.422471 0.178482 0.047561 1.035635
3 0.059468 0.047405 0.051626 0.285080 1.042828 0.297282 0.428757 0.183832 0.047405 1.042828
4 0.032684 0.038351 0.048928 0.245009 0.989755 0.270946 0.411558 0.169380 0.032684 0.989755
5 0.043561 0.072305 0.068141 0.230281 1.164807 0.315819 0.480329 0.230716 0.043561 1.164807
6 0.082703 0.070086 0.052686 0.259997 1.163968 0.325888 0.475902 0.226483 0.052686 1.163968
7 0.034529 0.059071 0.022520 0.235574 0.971924 0.264723 0.404638 0.163732 0.022520 0.971924
8 0.042204 0.025418 0.066486 0.274210 0.970355 0.275735 0.401086 0.160870 0.025418 0.970355
9 0.064815 0.039218 0.099146 0.279214 1.115388 0.319556 0.454708 0.206760 0.039218 1.115388
10 0.028852 0.035649 0.078844 0.292869 0.960992 0.279441 0.395967 0.156790 0.028852 0.960992
11 0.034370 0.066801 0.047832 0.247040 1.062028 0.291614 0.439224 0.192917 0.034370 1.062028
12 0.044788 0.036548 0.066373 0.329328 0.949151 0.285238 0.390605 0.152572 0.036548 0.949151
13 0.090509 0.075467 0.094019 0.186177 1.317020 0.352638 0.540870 0.292540 0.075467 1.317020
14 0.039060 0.040785 0.084241 0.242107 1.090651 0.299369 0.450087 0.202578 0.039060 1.090651
15 0.027008 0.094852 0.063980 0.262093 1.089606 0.307508 0.446360 0.199237 0.027008 1.089606
16 0.051717 0.107500 0.058318 0.366065 1.049094 0.326539 0.423997 0.179774 0.051717 1.049094
17 0.059056 0.105731 0.065285 0.316432 1.128456 0.334992 0.455898 0.207843 0.059056 1.128456
18 0.045102 0.073012 0.060312 0.284514 1.082873 0.309163 0.443468 0.196664 0.045102 1.082873
19 0.046701 0.062353 0.070722 0.248787 1.135034 0.312720 0.466985 0.218075 0.046701 1.135034
In [38]:
from sklearn.preprocessing import MinMaxScaler

# Create an instance of the MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))

# Normalize the features in the DataFrame
normalized_df_combi_open = pd.DataFrame(scaler.fit_transform(df_combi_open), columns=df_combi_open.columns)
normalized_df_combi_open
Out[38]:
delta theta alpha beta entropy mean std var min max
0 0.293677 0.333942 1.000000 0.514545 0.465643 0.653049 0.435453 0.395795 0.436979 0.465643
1 0.737736 0.853481 0.607624 0.409353 0.749597 0.928023 0.709356 0.676097 0.969551 0.749597
2 0.323664 0.477075 0.518627 0.801219 0.235095 0.509389 0.212067 0.185111 0.472942 0.235095
3 0.511171 0.267866 0.321643 0.549806 0.254649 0.370337 0.253896 0.223337 0.470007 0.254649
4 0.089393 0.157553 0.291835 0.327050 0.110378 0.070775 0.139442 0.120084 0.191975 0.110378
5 0.260674 0.571222 0.504146 0.245175 0.586230 0.581194 0.597107 0.558299 0.397396 0.586230
6 0.877083 0.544180 0.333359 0.410365 0.583949 0.695724 0.567646 0.528055 0.569740 0.583949
7 0.118434 0.409985 0.000000 0.274598 0.061906 0.000000 0.093389 0.079730 0.000000 0.061906
8 0.239308 0.000000 0.485866 0.489376 0.057639 0.125249 0.069747 0.059281 0.054743 0.057639
9 0.595386 0.168121 0.846783 0.517195 0.451891 0.623705 0.426601 0.387140 0.315376 0.451891
10 0.029043 0.124640 0.622428 0.593100 0.032187 0.167408 0.035684 0.030133 0.119596 0.032187
11 0.115935 0.504161 0.279714 0.338339 0.306840 0.305871 0.323552 0.288245 0.223807 0.306840
12 0.279995 0.135596 0.484609 0.795781 0.000000 0.233342 0.000000 0.000000 0.264952 0.000000
13 1.000000 0.609738 0.790123 0.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
14 0.189791 0.187214 0.682070 0.310915 0.384647 0.394078 0.395845 0.357265 0.312385 0.384647
15 0.000000 0.845908 0.458168 0.422017 0.381808 0.486657 0.371044 0.333397 0.084763 0.381808
16 0.389114 1.000000 0.395594 1.000000 0.271680 0.703127 0.222224 0.194341 0.551438 0.271680
17 0.504688 0.978445 0.472584 0.724089 0.487417 0.799280 0.434522 0.394884 0.690050 0.487417
18 0.284948 0.579833 0.417633 0.546657 0.363503 0.505480 0.351800 0.315013 0.426510 0.363503
19 0.310123 0.449978 0.532676 0.348051 0.505296 0.545938 0.508301 0.467983 0.456702 0.505296

Labeling¶

In [39]:
# Targeting
df_hold_target=pd.DataFrame(normalized_df_combi_hold)
df_hold_target['target']= 0
df_hold_target
Out[39]:
delta theta alpha beta entropy mean std var min max target
0 0.224576 0.743703 0.409782 0.253505 0.747335 0.609249 0.738726 0.710168 0.431980 0.747335 0
1 0.381333 0.338677 0.085124 0.000000 0.812714 0.379995 0.855938 0.837693 0.733507 0.812714 0
2 0.779967 0.453134 0.511401 0.199298 1.000000 1.000000 0.975105 0.971513 1.000000 1.000000 0
3 0.452898 0.580668 0.320354 0.047774 0.986933 0.769318 1.000000 1.000000 0.871165 0.986933 0
4 0.591392 0.324916 0.000000 0.801978 0.217430 0.234292 0.209173 0.184698 0.652508 0.217430 0
5 0.309246 0.439091 0.303634 0.719378 0.305651 0.302442 0.292530 0.261909 0.594845 0.305651 0
6 0.442711 0.198220 0.107479 0.483037 0.343801 0.118414 0.350628 0.316940 0.572332 0.343801 0
7 0.424288 0.350285 0.542886 0.722320 0.424520 0.518912 0.395042 0.359682 0.816133 0.424520 0
8 0.639851 0.329494 0.123062 0.590514 0.459445 0.433308 0.441368 0.404887 0.792570 0.459445 0
9 0.325833 1.000000 0.456968 0.406228 0.787465 0.873475 0.744997 0.716888 0.626752 0.787465 0
10 0.000000 0.649820 0.472184 1.000000 0.000000 0.118739 0.000000 0.000000 0.000000 0.000000 0
11 0.256445 0.000000 1.000000 0.536192 0.307335 0.210594 0.292761 0.262125 0.239779 0.307335 0
12 0.432175 0.630441 0.035964 0.590179 0.444685 0.411104 0.427536 0.391323 0.705872 0.444685 0
13 0.363396 0.090332 0.226108 0.475100 0.272434 0.000000 0.284786 0.254649 0.391329 0.272434 0
14 0.268006 0.347817 0.650422 0.961489 0.192397 0.372999 0.173976 0.152713 0.515518 0.192397 0
15 0.453162 0.366415 0.095409 0.356693 0.550014 0.331533 0.554199 0.517644 0.794077 0.550014 0
16 1.000000 0.016246 0.072636 0.849767 0.189056 0.291775 0.169839 0.148977 0.267036 0.189056 0
17 0.565782 0.653814 0.082959 0.196399 0.851867 0.702344 0.848844 0.829860 0.775603 0.851867 0
18 0.434821 0.485950 0.327598 0.434209 0.632647 0.567366 0.615854 0.580850 0.836392 0.632647 0
19 0.302703 0.399471 0.478666 0.655747 0.391868 0.394007 0.372636 0.338047 0.582260 0.391868 0
In [40]:
# Targeting
df_open_target=pd.DataFrame(normalized_df_combi_open)
df_open_target['target']= 1
df_open_target
Out[40]:
delta theta alpha beta entropy mean std var min max target
0 0.293677 0.333942 1.000000 0.514545 0.465643 0.653049 0.435453 0.395795 0.436979 0.465643 1
1 0.737736 0.853481 0.607624 0.409353 0.749597 0.928023 0.709356 0.676097 0.969551 0.749597 1
2 0.323664 0.477075 0.518627 0.801219 0.235095 0.509389 0.212067 0.185111 0.472942 0.235095 1
3 0.511171 0.267866 0.321643 0.549806 0.254649 0.370337 0.253896 0.223337 0.470007 0.254649 1
4 0.089393 0.157553 0.291835 0.327050 0.110378 0.070775 0.139442 0.120084 0.191975 0.110378 1
5 0.260674 0.571222 0.504146 0.245175 0.586230 0.581194 0.597107 0.558299 0.397396 0.586230 1
6 0.877083 0.544180 0.333359 0.410365 0.583949 0.695724 0.567646 0.528055 0.569740 0.583949 1
7 0.118434 0.409985 0.000000 0.274598 0.061906 0.000000 0.093389 0.079730 0.000000 0.061906 1
8 0.239308 0.000000 0.485866 0.489376 0.057639 0.125249 0.069747 0.059281 0.054743 0.057639 1
9 0.595386 0.168121 0.846783 0.517195 0.451891 0.623705 0.426601 0.387140 0.315376 0.451891 1
10 0.029043 0.124640 0.622428 0.593100 0.032187 0.167408 0.035684 0.030133 0.119596 0.032187 1
11 0.115935 0.504161 0.279714 0.338339 0.306840 0.305871 0.323552 0.288245 0.223807 0.306840 1
12 0.279995 0.135596 0.484609 0.795781 0.000000 0.233342 0.000000 0.000000 0.264952 0.000000 1
13 1.000000 0.609738 0.790123 0.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1
14 0.189791 0.187214 0.682070 0.310915 0.384647 0.394078 0.395845 0.357265 0.312385 0.384647 1
15 0.000000 0.845908 0.458168 0.422017 0.381808 0.486657 0.371044 0.333397 0.084763 0.381808 1
16 0.389114 1.000000 0.395594 1.000000 0.271680 0.703127 0.222224 0.194341 0.551438 0.271680 1
17 0.504688 0.978445 0.472584 0.724089 0.487417 0.799280 0.434522 0.394884 0.690050 0.487417 1
18 0.284948 0.579833 0.417633 0.546657 0.363503 0.505480 0.351800 0.315013 0.426510 0.363503 1
19 0.310123 0.449978 0.532676 0.348051 0.505296 0.545938 0.508301 0.467983 0.456702 0.505296 1
In [41]:
df_combi = pd.concat([normalized_df_combi_hold, normalized_df_combi_open])
df_combi
Out[41]:
delta theta alpha beta entropy mean std var min max target
0 0.224576 0.743703 0.409782 0.253505 0.747335 0.609249 0.738726 0.710168 0.431980 0.747335 0
1 0.381333 0.338677 0.085124 0.000000 0.812714 0.379995 0.855938 0.837693 0.733507 0.812714 0
2 0.779967 0.453134 0.511401 0.199298 1.000000 1.000000 0.975105 0.971513 1.000000 1.000000 0
3 0.452898 0.580668 0.320354 0.047774 0.986933 0.769318 1.000000 1.000000 0.871165 0.986933 0
4 0.591392 0.324916 0.000000 0.801978 0.217430 0.234292 0.209173 0.184698 0.652508 0.217430 0
5 0.309246 0.439091 0.303634 0.719378 0.305651 0.302442 0.292530 0.261909 0.594845 0.305651 0
6 0.442711 0.198220 0.107479 0.483037 0.343801 0.118414 0.350628 0.316940 0.572332 0.343801 0
7 0.424288 0.350285 0.542886 0.722320 0.424520 0.518912 0.395042 0.359682 0.816133 0.424520 0
8 0.639851 0.329494 0.123062 0.590514 0.459445 0.433308 0.441368 0.404887 0.792570 0.459445 0
9 0.325833 1.000000 0.456968 0.406228 0.787465 0.873475 0.744997 0.716888 0.626752 0.787465 0
10 0.000000 0.649820 0.472184 1.000000 0.000000 0.118739 0.000000 0.000000 0.000000 0.000000 0
11 0.256445 0.000000 1.000000 0.536192 0.307335 0.210594 0.292761 0.262125 0.239779 0.307335 0
12 0.432175 0.630441 0.035964 0.590179 0.444685 0.411104 0.427536 0.391323 0.705872 0.444685 0
13 0.363396 0.090332 0.226108 0.475100 0.272434 0.000000 0.284786 0.254649 0.391329 0.272434 0
14 0.268006 0.347817 0.650422 0.961489 0.192397 0.372999 0.173976 0.152713 0.515518 0.192397 0
15 0.453162 0.366415 0.095409 0.356693 0.550014 0.331533 0.554199 0.517644 0.794077 0.550014 0
16 1.000000 0.016246 0.072636 0.849767 0.189056 0.291775 0.169839 0.148977 0.267036 0.189056 0
17 0.565782 0.653814 0.082959 0.196399 0.851867 0.702344 0.848844 0.829860 0.775603 0.851867 0
18 0.434821 0.485950 0.327598 0.434209 0.632647 0.567366 0.615854 0.580850 0.836392 0.632647 0
19 0.302703 0.399471 0.478666 0.655747 0.391868 0.394007 0.372636 0.338047 0.582260 0.391868 0
0 0.293677 0.333942 1.000000 0.514545 0.465643 0.653049 0.435453 0.395795 0.436979 0.465643 1
1 0.737736 0.853481 0.607624 0.409353 0.749597 0.928023 0.709356 0.676097 0.969551 0.749597 1
2 0.323664 0.477075 0.518627 0.801219 0.235095 0.509389 0.212067 0.185111 0.472942 0.235095 1
3 0.511171 0.267866 0.321643 0.549806 0.254649 0.370337 0.253896 0.223337 0.470007 0.254649 1
4 0.089393 0.157553 0.291835 0.327050 0.110378 0.070775 0.139442 0.120084 0.191975 0.110378 1
5 0.260674 0.571222 0.504146 0.245175 0.586230 0.581194 0.597107 0.558299 0.397396 0.586230 1
6 0.877083 0.544180 0.333359 0.410365 0.583949 0.695724 0.567646 0.528055 0.569740 0.583949 1
7 0.118434 0.409985 0.000000 0.274598 0.061906 0.000000 0.093389 0.079730 0.000000 0.061906 1
8 0.239308 0.000000 0.485866 0.489376 0.057639 0.125249 0.069747 0.059281 0.054743 0.057639 1
9 0.595386 0.168121 0.846783 0.517195 0.451891 0.623705 0.426601 0.387140 0.315376 0.451891 1
10 0.029043 0.124640 0.622428 0.593100 0.032187 0.167408 0.035684 0.030133 0.119596 0.032187 1
11 0.115935 0.504161 0.279714 0.338339 0.306840 0.305871 0.323552 0.288245 0.223807 0.306840 1
12 0.279995 0.135596 0.484609 0.795781 0.000000 0.233342 0.000000 0.000000 0.264952 0.000000 1
13 1.000000 0.609738 0.790123 0.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1
14 0.189791 0.187214 0.682070 0.310915 0.384647 0.394078 0.395845 0.357265 0.312385 0.384647 1
15 0.000000 0.845908 0.458168 0.422017 0.381808 0.486657 0.371044 0.333397 0.084763 0.381808 1
16 0.389114 1.000000 0.395594 1.000000 0.271680 0.703127 0.222224 0.194341 0.551438 0.271680 1
17 0.504688 0.978445 0.472584 0.724089 0.487417 0.799280 0.434522 0.394884 0.690050 0.487417 1
18 0.284948 0.579833 0.417633 0.546657 0.363503 0.505480 0.351800 0.315013 0.426510 0.363503 1
19 0.310123 0.449978 0.532676 0.348051 0.505296 0.545938 0.508301 0.467983 0.456702 0.505296 1

Heatmap¶

In [42]:
import seaborn as sns

# Compute the correlation matrix
correlation_matrix = df_combi.corr()

# Create a heatmap with labels
plt.figure(figsize=(10, 8))
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')

# Add labels to the x and y axis ticks
plt.xticks(range(len(correlation_matrix.columns)), correlation_matrix.columns)
plt.yticks(range(len(correlation_matrix.columns)), correlation_matrix.columns)

# Display the plot
plt.show()

Classification¶

Splitting Data¶

In [43]:
#Splitting Data into features and target
x=df_combi.loc[:,'delta':'max'].to_numpy() #features
y=df_combi.loc[:,'target'].to_numpy() #target
In [44]:
import collections
counter = collections.Counter(y)
for label, _ in counter.items():
    row_ix = np.where(y == label)[0]
    plt.scatter(x[row_ix, 0], x[row_ix, 1], label=label)
plt.legend()
plt.show()
In [45]:
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

Training Model¶

In [46]:
# Training Model - Melakukan training model dengan menggunakan k = 1
from sklearn.neighbors import KNeighborsClassifier
k1 = 1
model_knn_1 = KNeighborsClassifier(n_neighbors = k1)
model_knn_1.fit(x_train, y_train)
Out[46]:
KNeighborsClassifier(n_neighbors=1)
In [47]:
# Predicting
y_pred_1 = model_knn_1.predict(x_test)
print(y_pred_1)
[1 1 1 1 1 0 0 1 0 1 1 1]
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [48]:
# Perhitungan Akurasi k = 1
from sklearn import metrics
print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_1.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_1))
Train set Accuracy :  1.0
Test set Accuracy :  0.6666666666666666
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [49]:
# Training Model - Melakukan training model dengan menggunakan k = 2
from sklearn.neighbors import KNeighborsClassifier
k2 = 2
model_knn_2 = KNeighborsClassifier(n_neighbors = k2)
model_knn_2.fit(x_train, y_train)
Out[49]:
KNeighborsClassifier(n_neighbors=2)
In [50]:
# Predicting
y_pred_2 = model_knn_2.predict(x_test)
print(y_pred_2)
[1 0 1 0 1 0 0 0 0 1 0 1]
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [51]:
# Perhitungan Akurasi K = 2
from sklearn import metrics
print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_2.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_2))
Train set Accuracy :  0.7857142857142857
Test set Accuracy :  0.8333333333333334
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [52]:
# Training Model - Melakukan training model dengan menggunakan k = 3
from sklearn.neighbors import KNeighborsClassifier
k3 = 3
model_knn_3 = KNeighborsClassifier(n_neighbors = k3)
model_knn_3.fit(x_train, y_train)
Out[52]:
KNeighborsClassifier(n_neighbors=3)
In [53]:
# Predicting
y_pred_3 = model_knn_3.predict(x_test)
print(y_pred_3)
[1 1 1 1 1 0 0 0 0 1 1 1]
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [54]:
# Perhitungan Akurasi K = 3
from sklearn import metrics
print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_3.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_3))
Train set Accuracy :  0.7857142857142857
Test set Accuracy :  0.75
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [55]:
# Training Model - Melakukan training model dengan menggunakan k = 4

from sklearn.neighbors import KNeighborsClassifier

k4 = 4

model_knn_4 = KNeighborsClassifier(n_neighbors = k4)
model_knn_4.fit(x_train, y_train)
Out[55]:
KNeighborsClassifier(n_neighbors=4)
In [56]:
# Predicting

y_pred_4 = model_knn_4.predict(x_test)
print(y_pred_4)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
[0 1 1 1 1 0 0 0 0 1 0 1]
In [57]:
# Perhitungan Akurasi K = 4

from sklearn import metrics

print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_4.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_4))
Train set Accuracy :  0.75
Test set Accuracy :  0.75
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [58]:
# Training Model - Melakukan training model dengan menggunakan k = 5

from sklearn.neighbors import KNeighborsClassifier

k5 = 5

model_knn_5 = KNeighborsClassifier(n_neighbors = k5)
model_knn_5.fit(x_train, y_train)
Out[58]:
KNeighborsClassifier()
In [59]:
# Predicting

y_pred_5 = model_knn_5.predict(x_test)
print(y_pred_5)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
[1 1 1 1 1 0 0 0 0 1 0 1]
In [60]:
# Perhitungan Akurasi K = 5

from sklearn import metrics

print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_5.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_5))
Train set Accuracy :  0.7857142857142857
Test set Accuracy :  0.8333333333333334
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [61]:
# Training Model - Melakukan training model dengan menggunakan k = 6

from sklearn.neighbors import KNeighborsClassifier

k6 = 6

model_knn_6 = KNeighborsClassifier(n_neighbors = k6)
model_knn_6.fit(x_train, y_train)
Out[61]:
KNeighborsClassifier(n_neighbors=6)
In [62]:
# Predicting

y_pred_6 = model_knn_6.predict(x_test)
print(y_pred_6)
[0 1 1 1 1 0 0 0 0 1 0 1]
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [63]:
# Perhitungan Akurasi K = 6

from sklearn import metrics

print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_6.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_6))
Train set Accuracy :  0.8214285714285714
Test set Accuracy :  0.75
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [64]:
# Training Model - Melakukan training model dengan menggunakan k = 7

from sklearn.neighbors import KNeighborsClassifier

k7 = 7

model_knn_7 = KNeighborsClassifier(n_neighbors = k7)
model_knn_7.fit(x_train, y_train)
Out[64]:
KNeighborsClassifier(n_neighbors=7)
In [65]:
# Predicting

y_pred_7 = model_knn_7.predict(x_test)
print(y_pred_7)
[1 1 1 1 1 0 0 0 0 1 0 1]
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [66]:
# Perhitungan Akurasi K = 7

from sklearn import metrics

print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_7.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_7))
Train set Accuracy :  0.7857142857142857
Test set Accuracy :  0.8333333333333334
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [67]:
# Training Model - Melakukan training model dengan menggunakan k = 8

from sklearn.neighbors import KNeighborsClassifier

k8 = 8

model_knn_8 = KNeighborsClassifier(n_neighbors = k8)
model_knn_8.fit(x_train, y_train)
Out[67]:
KNeighborsClassifier(n_neighbors=8)
In [68]:
# Predicting

y_pred_8 = model_knn_8.predict(x_test)
print(y_pred_8)
[0 1 1 1 1 0 0 0 0 1 0 1]
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [69]:
# Perhitungan Akurasi K = 8

from sklearn import metrics

print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_8.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_8))
Train set Accuracy :  0.7857142857142857
Test set Accuracy :  0.75
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [70]:
# Training Model - Melakukan training model dengan menggunakan k = 9

from sklearn.neighbors import KNeighborsClassifier

k9 = 9

model_knn_9 = KNeighborsClassifier(n_neighbors = k9)
model_knn_9.fit(x_train, y_train)
Out[70]:
KNeighborsClassifier(n_neighbors=9)
In [71]:
# Predicting

y_pred_9 = model_knn_9.predict(x_test)
print(y_pred_9)
[1 1 1 1 1 0 0 0 0 1 0 1]
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [72]:
# Perhitungan Akurasi K = 9

from sklearn import metrics

print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_9.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_9))
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
Train set Accuracy :  0.7857142857142857
Test set Accuracy :  0.8333333333333334
In [73]:
# Training Model - Melakukan training model dengan menggunakan k = 10

from sklearn.neighbors import KNeighborsClassifier

k10 = 10

model_knn_10 = KNeighborsClassifier(n_neighbors = k10)
model_knn_10.fit(x_train, y_train)
Out[73]:
KNeighborsClassifier(n_neighbors=10)
In [74]:
# Predicting

y_pred_10 = model_knn_10.predict(x_test)
print(y_pred_10)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
[1 1 1 1 1 0 0 0 0 1 0 1]
In [75]:
# Perhitungan Akurasi K = 10

from sklearn import metrics

print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn_10.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred_10))
Train set Accuracy :  0.8214285714285714
Test set Accuracy :  0.8333333333333334
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)

Evaluation¶

In [76]:
model_eval = pd.DataFrame(columns=['Train Accuracy', 'Test Accuracy'])

# Train Set Accuracy
model_eval.loc['K = 1','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_1.predict(x_train))
model_eval.loc['K = 2','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_2.predict(x_train))
model_eval.loc['K = 3','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_3.predict(x_train))
model_eval.loc['K = 4','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_4.predict(x_train))
model_eval.loc['K = 5','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_5.predict(x_train))
model_eval.loc['K = 6','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_6.predict(x_train))
model_eval.loc['K = 7','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_7.predict(x_train))
model_eval.loc['K = 8','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_8.predict(x_train))
model_eval.loc['K = 9','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_9.predict(x_train))
model_eval.loc['K = 10','Train Accuracy'] = metrics.accuracy_score(y_train, model_knn_10.predict(x_train))
model_eval 

# Test Set Accuracy 
model_eval.loc['K = 1','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_1)
model_eval.loc['K = 2','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_2)
model_eval.loc['K = 3','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_3)
model_eval.loc['K = 4','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_4)
model_eval.loc['K = 5','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_5)
model_eval.loc['K = 6','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_6)
model_eval.loc['K = 7','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_7)
model_eval.loc['K = 8','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_8)
model_eval.loc['K = 9','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_9)
model_eval.loc['K = 10','Test Accuracy'] = metrics.accuracy_score(y_test, y_pred_10)
model_eval 
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
Out[76]:
Train Accuracy Test Accuracy
K = 1 1.0 0.666667
K = 2 0.785714 0.833333
K = 3 0.785714 0.75
K = 4 0.75 0.75
K = 5 0.785714 0.833333
K = 6 0.821429 0.75
K = 7 0.785714 0.833333
K = 8 0.785714 0.75
K = 9 0.785714 0.833333
K = 10 0.821429 0.833333

Retraining¶

In [81]:
# Retraining Menggunakan Nilai K dengan Akurasi Terbaik - K = 10

k = 10

model_knn = KNeighborsClassifier(n_neighbors = k)
model_knn.fit(x_train, y_train)
Out[81]:
KNeighborsClassifier(n_neighbors=10)
In [82]:
# Predicting
y_pred = model_knn.predict(x_test)
print(y_pred)
[1 1 1 1 1 0 0 0 0 1 0 1]
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)
In [83]:
# Perhitungan Akurasi k = 10

print("Train set Accuracy : ", metrics.accuracy_score(y_train, model_knn.predict(x_train)))
print("Test set Accuracy : ", metrics.accuracy_score(y_test, y_pred))
print()
print(f"Achieved accuracy of {metrics.accuracy_score(y_test, y_pred)*100}% on test data")
Train set Accuracy :  0.8214285714285714
Test set Accuracy :  0.8333333333333334

Achieved accuracy of 83.33333333333334% on test data
C:\Users\CLARA\anaconda3\lib\site-packages\sklearn\neighbors\_classification.py:228: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behavior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warning.
  mode, _ = stats.mode(_y[neigh_ind, k], axis=1)

Confusion Matrix¶

In [84]:
# Confusion Matrix
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
import seaborn as sns
target_names = ['hold hand', 'open hand']
print(classification_report(y_test, y_pred, target_names=target_names))

f, ax = plt.subplots(figsize=(8,5))
cm = confusion_matrix(y_test, y_pred)
cm_labels = ['hold hand', 'open hand']
sns.heatmap(cm,cmap= "Blues", linecolor = 'black' , linewidth = 1 , annot = True, fmt='', 
            yticklabels = cm_labels,
            xticklabels = cm_labels, ax=ax)
plt.xlabel("Actual Values")
plt.ylabel("Predicted Values")
plt.show()
              precision    recall  f1-score   support

   hold hand       1.00      0.71      0.83         7
   open hand       0.71      1.00      0.83         5

    accuracy                           0.83        12
   macro avg       0.86      0.86      0.83        12
weighted avg       0.88      0.83      0.83        12